package com.superme.repository;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SpecificationBase {
    protected Predicate SetAnsCondition(Predicate pQuery, CriteriaBuilder cb, Object Param, Predicate pNew) {
        if (Param != null) {
            //Predicate pName = cb.like(_path, "%"+this.name+"%");
            if (pQuery != null) {
                pQuery = cb.and(pNew, pQuery);
            } else {
                pQuery = pNew;
            }
        }
        return pQuery;
    }

    protected Predicate SetOrConditions(Predicate pQuery, CriteriaBuilder cb, Object Param, Predicate pNew) {
        if (Param != null) {
            //Predicate pName = cb.like(_path, "%"+this.name+"%");
            if (pQuery != null) {
                pQuery = cb.or(pNew, pQuery);
            } else {
                pQuery = pNew;
            }
        }
        return pQuery;
    }

    protected boolean isNumeric(String str) {
        if (str.trim() != "") {
            Pattern pattern = Pattern.compile("[0-9]*");
            Matcher isNum = pattern.matcher(str);
            return isNum.matches();
        }
        return false;
    }


    protected boolean isDateTime(String str) {
        if (str.trim() != "") {
            Pattern pattern = Pattern.compile("^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1-2][0-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$");
            Matcher isDateTime = pattern.matcher(str);
            return isDateTime.matches();
        }
        return false;
    }

    protected boolean isDate(String str) {
        if (str.trim() != "") {
            Pattern pattern = Pattern.compile("^((\\d{2}(([02468][048])|([13579][26]))[\\-\\-\\s]?((((0?" +"[13578])|(1[02]))[\\-\\-\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))" +"|(((0?[469])|(11))[\\-\\-\\s]?((0?[1-9])|([1-2][0-9])|(30)))|" +"(0?2[\\-\\-\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][12" +"35679])|([13579][01345789]))[\\-\\-\\s]?((((0?[13578])|(1[02]))" +"[\\-\\-\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))" +"[\\-\\-\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\-\\s]?((0?[" +"1-9])|(1[0-9])|(2[0-8]))))))");
            Matcher isDate = pattern.matcher(str);
            return isDate.matches();
        }
        return false;
    }


}
